import 'babel-polyfill'
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './plugins/element.js'
import qs from 'qs'




//导入全局样式表
import './assets/css/global.css'

import axios from 'axios'



//配置请求根路径
axios.defaults.baseURL = 'https://produce.mstar.cn/'
//axios.defaults.headers = { 'Content-Type': 'application/json' }
//判断是否有权限
// async function token() {
//   const { data: res } = await axios.get("produce/get_product_group.php")
//   console.log(res)
// }
//http://timemeetyou.com:8889/api/private/v1/

//获取token
async function token() {
  var time = Date.parse(new Date()).toString();
  let tokentome = ''
  let gettoken = { appid: 'produce@mstar.cn', secert: 'c811385fe41607e64d816db509f33ffb', timestamp: time.substr(0, 10) }
  //console.log(gettoken)
  await axios.post('api/get_token.php', gettoken, {


  })
    .then(function (response) {
      tokentome = response.data.data.token


    })
    .catch(function (error) {

    });

  return tokentome

}




//配置请求拦截器
axios.interceptors.request.use(async config => {
  //console.log(config)


  if (config.url !== "api/get_token.php") {
    //获取token加在请求头部
    if (config.method == 'post') {
      let i = config.data;
      await token().then(data => {
        i = Object.assign(i, {
          token: data
        })
        config.data = i

      })

    }
    // else if (config.method == 'get') {
    //   let i = config.params;
    //   await token().then(data => {
    //     i = Object.assign(i, {
    //       token: data
    //     })
    //     config.params = i
    //     console.log(config)

    //   })

    // }




    //console.log(config.data)

  }

  return config





  // console.log(config)


  //console.log(config)
  //   //在headers的自定义字段中加入客户端的token
  //   config.headers.Authorization = window.sessionStorage.getItem('token')
  //   //在最后必须return config



})

//配置响应拦截器
axios.interceptors.response.use(config => {


  //当响应拦截到token失效或不存在时跳转
  //if (config.data.code == -5) return router.push("/home");
  //console.log(config.data.code)
  return config
})



Vue.prototype.$http = axios

Vue.config.productionTip = false
// 全局注册处理时间过滤器
Vue.filter('dataFormat', function (originVal) {
  const dt = new Date(originVal)

  const y = dt.getFullYear()
  const m = (dt.getMonth() + 1 + '').padStart(2, '0')
  const d = (dt.getDate() + '').padStart(2, '0')

  const hh = (dt.getHours() + '').padStart(2, '0')
  const mm = (dt.getMinutes() + '').padStart(2, '0')
  const ss = (dt.getSeconds() + '').padStart(2, '0')
  // yyyy-mm-dd hh:mm:ss
  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
})
new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
